package org.lsy.config;

import com.rabbitmq.client.impl.AMQImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author liusy95
 * @version 1.0
 * @date 2021/1/13
 */
@Configuration
@Slf4j
public class RabbitConfig {

    @Bean
    public Queue queue() {
        Map<String, Object> properties = new HashMap(10);
        // 消息的生存时间 10s
        properties.put("x-message-ttl", 10000);
        // 设置该队列所关联的死信交换器（当队列消息TTL到期后依然没有消费，则加入死信队列）
        properties.put("x-dead-letter-exchange", "ex.order.dlx");
        // 设置该队列所关联的死信交换器的routingKey，如果没有特殊指定，使用原队列的routingKey
        properties.put("x-dead-letter-routing-key", "order.dlx");

        Queue queue = new Queue("q.order", true, false, false, properties);
        return queue;
    }

    @Bean
    public Queue dlxQueue() {
        Queue queue = new Queue("q.order.dlx", true, false, false);
        return queue;
    }

    @Bean
    public DirectExchange exchange() {
        DirectExchange exchange = new DirectExchange("ex.order", true, false);
        return exchange;
    }

    @Bean
    public DirectExchange dlxExchange() {
        DirectExchange exchange = new DirectExchange("ex.order.dlx", true, false);
        return exchange;
    }

    @Bean
    public Binding binding() {
        Binding binding = BindingBuilder.bind(queue()).to(exchange()).with("order");
        return binding;
    }

    @Bean
    public Binding dlxBinding() {
        Binding binding = BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("order.dlx");
        return binding;
    }

}
